﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace YasharEl.Infrastructure.Caching.Configuration
{
    public partial class CacheManagerCollection
    {
        public static Dictionary<string, Type> CacheManagersSubclassesMap = new Dictionary<string, Type>();

        static CacheManagerCollection()
        {
            CacheManagersSubclassesMap.Add("ormCacheManagerConfig", typeof(OrmCacheManagerConfig));
            CacheManagersSubclassesMap.Add("webCacheManagerConfig", typeof(WebCacheManagerConfig));
            CacheManagersSubclassesMap.Add("applicationCacheManagerConfig", typeof(ApplicationCacheManagerConfig));
        }

        protected virtual bool HandleUnrecognizedElement(string elementName, XmlReader reader)
        {
            try
            {
                if (CacheManagersSubclassesMap.ContainsKey(elementName))
                {
                    Type cacheManagerConfigType = CacheManagersSubclassesMap[elementName];

                    CacheManagerConfig currentElement = (CacheManagerConfig)Activator.CreateInstance(cacheManagerConfigType);

                    currentElement.DeserializeElement(reader);

                    BaseAdd(currentElement, true);

                    return true;
                }
            }
            catch
            {
                return false;
            }

            return false;
        }
    }
}
